package wifiunion.com.notice.utils.glideutils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;

import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;

import wifiunion.com.notice.R;

/**
 * Created by Administrator on 2016/4/12.
 */
public class GlideCircleHasBackgroudTransform extends BitmapTransformation {

    /**
     * 0.null
     * 1.blue
     * 2.red
     * 3.yellow
     * 4.trans
     */
    private static int mBackgroudColor;

    public GlideCircleHasBackgroudTransform(Context context) {
        super(context);
    }

    private static int mType;

    private static Context mContext;

    public GlideCircleHasBackgroudTransform(Context context, int type) {
        super(context);
        mType = type;
        mContext = context;
        switch (type) {
            case 0:
                mBackgroudColor = context.getResources().getColor(R.color.start_circle_blue);
                break;
            case 1:
                mBackgroudColor = context.getResources().getColor(R.color.start_circle_blue);
                break;
            case 2:
                mBackgroudColor = context.getResources().getColor(R.color.start_circle_red);
                break;
            case 3:
                mBackgroudColor = context.getResources().getColor(R.color.start_circle_yellow);
                break;
            case 4:
                mBackgroudColor = context.getResources().getColor(R.color.start_circle_blue);
                break;
            default:
                mBackgroudColor = context.getResources().getColor(R.color.start_circle_blue);
                break;
        }
    }

    @Override
    protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
        return circleCrop(pool, toTransform);
    }

    private static Bitmap circleCrop(BitmapPool pool, Bitmap source) {
        if (source == null) return null;

        int size = Math.min(source.getWidth(), source.getHeight());
        int x = (source.getWidth() - size) / 2;
        int y = (source.getHeight() - size) / 2;

        // TODO this could be acquired from the pool too
        Bitmap squared = Bitmap.createBitmap(source, x, y, size, size);

        Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888);
        if (result == null) {
            result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
        }

        Canvas canvas = new Canvas(result);
        Paint paint = new Paint();
        paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
        paint.setAntiAlias(true);
        float r = size / 2f;
        canvas.drawCircle(r, r, r, paint);


        paint.reset();

        paint.setColor(mBackgroudColor);

        int circleDistance = (40 * size) / 800;

        LinearGradient gradient;
        switch (mType) {
            case 0:
            case 1:
                gradient = new LinearGradient(0, 0, (r - (circleDistance / 2)) * 2, (r - (circleDistance / 2)) * 2, mContext.getResources().getColor(R.color.start_circle_blue), mContext.getResources().getColor(R.color.end_circle_blue), Shader.TileMode.MIRROR);
                break;
            case 2:
                gradient = new LinearGradient(0, 0, (r - (circleDistance / 2)) * 2, (r - (circleDistance / 2)) * 2, mContext.getResources().getColor(R.color.start_circle_red), mContext.getResources().getColor(R.color.end_circle_red), Shader.TileMode.MIRROR);
                break;
            case 3:
                gradient = new LinearGradient(0, 0, (r - (circleDistance / 2)) * 2, (r - (circleDistance / 2)) * 2, mContext.getResources().getColor(R.color.start_circle_yellow), mContext.getResources().getColor(R.color.end_circle_yellow), Shader.TileMode.MIRROR);
                break;
            case 4:
                gradient = new LinearGradient(0, 0, (r - (circleDistance / 2)) * 2, (r - (circleDistance / 2)) * 2, mContext.getResources().getColor(R.color.start_circle_blue), mContext.getResources().getColor(R.color.end_circle_blue), Shader.TileMode.MIRROR);
                break;
            default:
                gradient = new LinearGradient(0, 0, (r - (circleDistance / 2)) * 2, (r - (circleDistance / 2)) * 2, mContext.getResources().getColor(R.color.start_circle_blue), mContext.getResources().getColor(R.color.end_circle_blue), Shader.TileMode.MIRROR);
                break;
        }
//        LinearGradient gradient = new LinearGradient(0, 0, (r-10)*2, (r-10)*2, 0xffff2959, 0xffff0c45, Shader.TileMode.MIRROR);
        paint.setShader(gradient);
        paint.setStrokeWidth(circleDistance);
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);
        canvas.drawCircle(r, r, r - (circleDistance / 2), paint);
        return result;
    }

    @Override
    public String getId() {
        return getClass().getName()+" "+mType;
    }
}
